package com.lagou.dao;

import com.lagou.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AccountDao {
    public static boolean signIn(String username, String pwd) {

        String sql = "SELECT userName,PASSWORD FROM account WHERE userName = ? AND PASSWORD = ? LIMIT 1";
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
//        String[] res = new String[2];
        boolean flag = false;
        try {
            con = JDBCUtils.getConnection();
            ps = con.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, pwd);
            resultSet = ps.executeQuery();
            if (resultSet.next()) {
//                res[0] = resultSet.getString(0);
//                res[1] = resultSet.getString(1);
                flag = true;
            } else {
                System.out.println("登录失败! ");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(con, ps, resultSet);
        }
        return flag;

    }

    public static boolean register(String username, String pwd) {

        String sql = "INSERT INTO Account (userName,password) VALUES(?,?)";
        Connection con = null;
        PreparedStatement ps = null;
        int flag = 0;
        try {
            con = JDBCUtils.getConnection();
            ps = con.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, pwd);
            flag = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(con, ps);
        }
        return flag > 0;

    }


    public static boolean check(String username) {

        String sql = "SELECT userName FROM account WHERE userName = ? LIMIT 1";
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        boolean flag = true;
        try {
            con = JDBCUtils.getConnection();
            ps = con.prepareStatement(sql);
            ps.setString(1, username);
            resultSet = ps.executeQuery();
            flag = resultSet.next();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(con, ps);
        }
        return flag;

    }


}
